<template>
  <div class="app-container">
    <el-row :gutter="20">

      <el-col :span="8" :xs="24">
        <el-card style="margin-bottom:20px;">
          <div slot="header" class="clearfix">
            <span>账号信息</span>
          </div>

          <div class="user-profile">
            <div class="box-center">
              <div class="pan-item" style="z-index: 1; height: 100px; width: 100px;">
                <div :style="{backgroundImage: `url(${user.avatar})`}" class="pan-thumb"></div>
              </div>
            </div>
            <div class="box-center">
              <div class="user-name text-center">{{ user.name }}</div>
              <div class="user-role text-center text-muted">{{ user.role | uppercaseFirst }}</div>
            </div>
          </div>
<!--
          <el-form style='width: 400px; margin-left:50px;'>
            <el-form-item label="Name">
              <el-input v-model.trim="user.name" />
            </el-form-item>
            <el-form-item label="Email">
              <el-input v-model.trim="user.email" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">Update</el-button>
            </el-form-item>
          </el-form>
-->

          <div class="box-center" style="margin-top: 50px">
            <el-button type="warning" size="small" @click="handleResetPassword()">重置密码</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog title="修改密码" :visible.sync="showChangePwdDialog">
      <el-form :rules="changePwdRules" ref="changePwdForm" :model="sysUser" label-position="left" label-width="200px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="当前密码" prop="oldPassword">
          <el-input type="password" v-model="sysUser.oldPassword" placeholder="请输入当前密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input type="password" v-model="sysUser.newPassword" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="sysUser.confirmPassword" placeholder="请再输入一次新密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showChangePwdDialog = false">取消</el-button>
        <el-button type="primary" @click="tryResetPassword">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { changeSelfPassword } from '@/api/user'

export default {
  name: 'Profile',
  components: { },
  data() {
    return {
      user: {},
      showChangePwdDialog: false,
      sysUser: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      changePwdRules: {
        oldPassword: [{ required: true, message: '当前密码不能为空。', trigger: 'blur' }],
        newPassword: [{ required: true, message: '新密码不能为空。', trigger: 'blur' },
          { min: 6, message: '新密码长度不能少于 6 位', trigger: 'blur' }],
        confirmPassword: [{ required: true, message: '确认密码不能为空。', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'avatar',
      'roles'
    ])
  },
  created() {
    this.getUser()
  },
  methods: {
    getUser() {
      this.user = {
        name: this.name,
        role: this.roles.join(' | '),
        avatar: this.avatar
      }
    },
    handleResetPassword() {
      // 设置界面数据
      this.sysUser.oldPassword = ''
      this.sysUser.newPassword = ''
      this.sysUser.confirmPassword = ''

      // 显示界面
      this.showChangePwdDialog = true
      this.$nextTick(() => {
        this.$refs['changePwdForm'].clearValidate()
      })
    },
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.sysUser.newPassword) {
        callback(new Error('新密码与确认密码不一致'));
      } else {
        callback();
      }
    },
    tryResetPassword() {
      //校验,然后执行
      this.$refs['changePwdForm'].validate(valid => {
        if (valid) {
          const params = {}
          params.oldPassword = this.sysUser.oldPassword
          params.newPassword = this.sysUser.newPassword
          changeSelfPassword(params).then(() => {
            //修改成功后，删除所有密码
            this.sysUser.oldPassword = ''
            this.sysUser.newPassword = ''
            this.sysUser.confirmPassword = ''
            this.showChangePwdDialog = false
            this.$notify({ title: '成功', message: '操作成功', type: 'success', duration: 2000 })
          })
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
  .pan-item {
    width: 200px;
    height: 200px;
    border-radius: 50%;
    display: inline-block;
    position: relative;
    cursor: default;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  }

  .pan-info-roles-container {
    padding: 20px;
    text-align: center;
  }

  .pan-thumb {
    width: 100%;
    height: 100%;
    background-position: center center;
    background-size: cover;
    border-radius: 50%;
    overflow: hidden;
    position: absolute;
    transform-origin: 95% 40%;
    transition: all 0.3s ease-in-out;
  }

  .box-center {
    margin: 0 auto;
    display: table;
  }

  .text-muted {
    color: #777;
  }

  .user-profile {
    .user-name {
      font-weight: bold;
    }

    .box-center {
      padding-top: 10px;
    }

    .user-role {
      padding-top: 10px;
      font-weight: 400;
      font-size: 14px;
    }

    .box-social {
      padding-top: 30px;

      .el-table {
        border-top: 1px solid #dfe6ec;
      }
    }

    .user-follow {
      padding-top: 20px;
    }
  }

  .user-bio {
    margin-top: 20px;
    color: #606266;

    span {
      padding-left: 4px;
    }

    .user-bio-section {
      font-size: 14px;
      padding: 15px 0;

      .user-bio-section-header {
        border-bottom: 1px solid #dfe6ec;
        padding-bottom: 10px;
        margin-bottom: 10px;
        font-weight: bold;
      }
    }
  }
</style>

